package com.itheima.leetcode.od.c.logicalsimulation;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * <h3>推荐多样性</h3>
 */
public class RecommendDiversity {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        int m = Integer.parseInt(in.nextLine());

        int[][] input_matrix = new int[10][100];
        int[] input_length = new int[10];
        int count = 0;
        while (true) {
            if (!in.hasNextLine()) {
                break;
            } else {
                int[] nums = split(in.nextLine(), " ");
                input_length[count] = nums.length;
                for (int i = 0; i < nums.length; i++) {
                    input_matrix[count][i] = nums[i];
                }
                count += 1;
            }
        }

        int[] output_length = new int[count];
        ArrayList<Integer> nums = new ArrayList<>();
        while (nums.size() < n * m) {
            for (int i = 0; i < count; i++) {
                int index = input_length[i];
                if (input_length[i] > output_length[i] + n) {
                    index = output_length[i] + n;
                }
                for (int j = output_length[i]; j < index; j++) {
                    nums.add(input_matrix[i][j]);
                }
                output_length[i] = index;
            }
        }

        String output_str = "";
        for (int j = 0; j < n; j++) {
            for (int i = 0; i < m; i++) {
                output_str += nums.get(i * n + j) + " ";
            }
        }
        System.out.println(output_str.substring(0, output_str.length() - 1));
    }

    /**
     * 分割字符串
     *
     * @param input_str
     * @param chars
     * @return
     */
    public static int[] split(String input_str, String chars) {
        String[] tmp2 = input_str.split(chars);
        int[] counts = new int[tmp2.length];
        for (int i = 0; i < tmp2.length; i++) {
            counts[i] = Integer.parseInt(tmp2[i]);
        }
        return counts;
    }

}